package com.krain.corelibrary.helper.imageloader.depend;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;

import java.lang.ref.WeakReference;

/**
 * 由http://developer.android.com/training/displaying-bitmaps/index.html<br>
 * 中BitmapFun.zip demo更改 A custom Drawable that will be attached to the
 * imageView while the work is in progress. Contains a reference to the actual
 * worker task, so that it can be stopped if a new binding is required, and
 * makes sure that only the last started worker process can bind its result,
 * independently of the finish order.
 */
public class AsyncDrawable extends BitmapDrawable {

    private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;

    public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
        super(res, bitmap);
        bitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
    }

    public BitmapWorkerTask getBitmapWorkerTask() {
        return bitmapWorkerTaskReference.get();
    }

    // 这里Override处理图片被recycle问题
    @Override
    public void draw(Canvas canvas) {

        // 判断bitmap是否被recycle
        Bitmap temp = getBitmap();
        if (temp == null || temp.isRecycled()) {
            canvas.drawColor(Color.TRANSPARENT);
            return;
        }

        super.draw(canvas);
    }
}
